package com.slj.service.impl;

import com.slj.entity.Products;
import com.slj.mapper.ProductsMapper;
import com.slj.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductsServiceImpl implements ProductsService {

    @Autowired
    private ProductsMapper productsMapper;

    // 根据主键删除商品
    @Override
    public int deleteByPrimaryKey(Integer productId) {
        return productsMapper.deleteByPrimaryKey(productId);
    }

    // 插入商品记录
    @Override
    public int insert(Products record) {
        return productsMapper.insert(record);
    }

    // 选择性插入商品记录
    @Override
    public int insertSelective(Products record) {
        return productsMapper.insertSelective(record);
    }

    // 根据主键查询商品
    @Override
    public Products selectByPrimaryKey(Integer productId) {
        return productsMapper.selectByPrimaryKey(productId);
    }

    // 选择性更新商品
    @Override
    public int updateByPrimaryKeySelective(Products record) {
        return productsMapper.updateByPrimaryKeySelective(record);
    }

    // 更新商品所有字段
    @Override
    public int updateByPrimaryKey(Products record) {
        return productsMapper.updateByPrimaryKey(record);
    }

    // 查询所有商品
    @Override
    public List<Products> selectAll() {
        return productsMapper.selectAll();
    }

    // 根据商品名称模糊查询
    @Override
    public List<Products> selectByProductName(String productName) {
        return productsMapper.selectByProductName(productName);
    }

    // 切换商品状态
    @Override
    public int toggleStatus(Integer productId) {
        // 先获取当前商品状态
        Products product = productsMapper.selectByPrimaryKey(productId);
        if (product == null) {
            return 0; // 商品不存在
        }

        // 切换状态
        String newStatus = "active".equals(product.getStatus()) ? "inactive" : "active";
        return productsMapper.updateStatus(productId, newStatus);
    }
}